<?php
//-- mod : aucp colors ---------------------------------------------------------
//-- mod : categories hierarchy ------------------------------------------------
/***************************************************************************
 *                             admin_groups.php
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id: admin_groups.php 5614 2006-03-09 19:42:41Z grahamje $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', 1);

if ( !empty($setmodules) )
{
	$filename = basename(__FILE__);
	$module['Groups']['Manage'] = $filename;

	return;
}

//
// Load default header
//
$phpbb_root_path = './../';
//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
require($phpbb_root_path . 'extension.inc');
*/
//-- add
$phpEx = substr(strrchr(__FILE__, '.'), 1);
$requester = 'admin/admin_groups';
//-- fin mod : categories hierarchy --------------------------------------------
require('./pagestart.' . $phpEx);

if ( isset($HTTP_POST_VARS[POST_GROUPS_URL]) || isset($HTTP_GET_VARS[POST_GROUPS_URL]) )
{
	$group_id = ( isset($HTTP_POST_VARS[POST_GROUPS_URL]) ) ? intval($HTTP_POST_VARS[POST_GROUPS_URL]) : intval($HTTP_GET_VARS[POST_GROUPS_URL]);
}
else
{
	$group_id = 0;
}

if ( isset($HTTP_POST_VARS['mode']) || isset($HTTP_GET_VARS['mode']) )
{
	$mode = ( isset($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];
	$mode = htmlspecialchars($mode);
}
else
{
	$mode = '';
}

//-- mod : categories hierarchy ------------------------------------------------
//-- add
include($config->url('includes/class_forums'));
include($config->url('includes/class_cp'));

// get styles available
$styles_available = get_list_styles_dft();

$user->get_cache(POST_GROUPS_URL);
$user_groups_list = $user->get_groups_list();

// auth
$auth_manage = false;

// level
$is_founder = $user->data['session_logged_in'] && in_array(GROUP_FOUNDER, $user_groups_list);
$is_admin = $user->data['session_logged_in'] && in_array(GROUP_ADMIN, $user_groups_list);
$is_owner = false;

// read group
if ( _button('new') || ($mode == 'newgroup') )
{
	$group_id = 0;
}
else
{
	$group_id = _read(POST_GROUPS_URL, TYPE_INT);
}
$group_status = GROUP_STANDARD;
$group_row = array();
if ( !empty($group_id) )
{
	$sql = 'SELECT *
				FROM ' . GROUPS_TABLE . '
				WHERE group_id = ' . intval($group_id) . '
					AND group_single_user <> ' . true . '
					AND group_status < ' . GROUP_SPECIAL;
	$result = $db->sql_query($sql, false, __LINE__, __FILE__);
	if ( $group_row = $db->sql_fetchrow($result) )
	{
		if ( !isset($styles_available[ $group_row['group_style'] ]) )
		{
			$group_row['group_style'] = 0;
		}
		$group_status = intval($group_row['group_status']);
		$is_owner = $user->data['session_logged_in'] && (intval($group_row['group_moderator']) == $user->data['user_id']);
	}
	$db->sql_freeresult($result);
}

switch ( $group_id )
{
	case GROUP_FOUNDER:
		$auth_manage = $is_owner;
		break;
	case GROUP_ADMIN:
		$auth_manage = $is_founder;
		break;
	case 0: // GROUP_ALL:
		if ( _button('new') || ($mode == 'newgroup') )
		{
			$auth_manage = $is_founder || $is_admin;
		}
	default:
		$auth_manage = $is_owner || $is_founder || $is_admin;
		break;
}
if ( !empty($group_id) || (!_button('new') && ($mode != 'newgroup')) )
{
	// check group
	if ( !empty($group_id) && empty($group_row) )
	{
		_error('Group_not_exist');
	}

	// check management auth
	if ( !$auth_manage )
	{
		_error('Manage_group_denied');
	}

	// send error
	if ( $error )
	{
		$l_link = 'Click_return_groupsadmin';
		$u_link = $config->url('admin/admin_groups', '', true);
		message_return($error_msg, $l_link, $u_link);
	}
}

// plugs
if ( $config->plug_ins[$requester] )
{
	foreach ( $config->plug_ins[$requester] as $plug => $dummy )
	{
		if ( method_exists($config->plug_ins[$requester][$plug], 'start') )
		{
			$config->plug_ins[$requester][$plug]->start();
		}
	}
}
//-- fin mod : categories hierarchy --------------------------------------------

if ( isset($HTTP_POST_VARS['edit']) || isset($HTTP_POST_VARS['new']) )
{
	//
	// Ok they are editing a group or creating a new group
	//
	$template->set_filenames(array(
		'body' => 'admin/group_edit_body.tpl')
	);

	if ( isset($HTTP_POST_VARS['edit']) )
	{
		//
		// They're editing. Grab the vars.
		//
//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
		$sql = "SELECT *
			FROM " . GROUPS_TABLE . "
			WHERE group_single_user <> " . TRUE . "
			AND group_id = $group_id";
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Error getting group information', '', __LINE__, __FILE__, $sql);
		}

		if ( !($group_info = $db->sql_fetchrow($result)) )
		{
			message_die(GENERAL_MESSAGE, $lang['Group_not_exist']);
		}
*/
//-- add
		$group_info = $group_row;
//-- fin mod : categories hierarchy --------------------------------------------

		$mode = 'editgroup';
		$template->assign_block_vars('group_edit', array());

	}
	else if ( isset($HTTP_POST_VARS['new']) )
	{
		$group_info = array (
			'group_name' => '',
			'group_description' => '',
			'group_moderator' => '',
			'group_type' => GROUP_OPEN);
		$group_open = ' checked="checked"';

		$mode = 'newgroup';

//-- mod : categories hierarchy ------------------------------------------------
//-- add
		$group_info['group_status'] = GROUP_STANDARD;
		$group_info['group_style'] = 0;
//-- fin mod : categories hierarchy --------------------------------------------

	}

	//
	// Ok, now we know everything about them, let's show the page.
	//
//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
	if ($group_info['group_moderator'] != '')
	{
		$sql = "SELECT user_id, username
			FROM " . USERS_TABLE . "
			WHERE user_id = " . $group_info['group_moderator'];
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain user info for moderator list', '', __LINE__, __FILE__, $sql);
		}

		if ( !($row = $db->sql_fetchrow($result)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain user info for moderator list', '', __LINE__, __FILE__, $sql);
		}

		$group_moderator = $row['username'];
	}
	else
	{
		$group_moderator = '';
	}

	$group_open = ( $group_info['group_type'] == GROUP_OPEN ) ? ' checked="checked"' : '';
	$group_closed = ( $group_info['group_type'] == GROUP_CLOSED ) ? ' checked="checked"' : '';
	$group_hidden = ( $group_info['group_type'] == GROUP_HIDDEN ) ? ' checked="checked"' : '';
*/
//-- add
	if ( $group_info['group_moderator'] == ANONYMOUS )
	{
		$group_info['group_moderator'] = 0;
	}
	if ( intval($group_info['group_moderator']) )
	{
		$sql = 'SELECT username
					FROM ' . USERS_TABLE . '
					WHERE user_id = ' . intval($group_info['group_moderator']);
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$group_moderator = ($row = $db->sql_fetchrow($result)) ? $row['username'] : '';
		$db->sql_freeresult($result);
	}
	else
	{
		$group_moderator = '';
	}

	$group_open = ( $group_info['group_type'] == GROUP_OPEN ) ? ' checked="checked"' : '';
	$group_closed = ( $group_info['group_type'] == GROUP_CLOSED ) ? ' checked="checked"' : '';
	$group_hidden = ( $group_info['group_type'] == GROUP_HIDDEN ) ? ' checked="checked"' : '';
	$s_group_sysstatus_std = (intval($group_info['group_status']) == GROUP_STANDARD) ? ' checked="checked"' : '';
	$s_group_sysstatus_sys = (intval($group_info['group_status']) != GROUP_STANDARD) ? ' checked="checked"' : '';
	$template->assign_vars(array(
		'L_GROUP_SYSSTATUS' => $user->lang('System_group'),
		'S_GROUP_SYSSTATUS_STD' => $s_group_sysstatus_std,
		'GROUP_SYSSTATUS_STD' => GROUP_STANDARD,
		'S_GROUP_SYSSTATUS_SYS' => $s_group_sysstatus_sys,
		'GROUP_SYSSTATUS_SYS' => GROUP_SYSTEM,
		'L_YES' => $user->lang('Yes'),
		'L_NO' => $user->lang('No'),
		'L_GROUP_STYLE' => $user->lang('Group_style'),
		'L_GROUP_STYLE_EXPLAIN' => $user->lang('Group_style_explain'),
	));
	foreach ( $styles_available as $style_id => $style_name )
	{
		$template->assign_block_vars('style', array(
			'STYLE_ID' => $style_id,
			'STYLE_NAME' => $user->lang($style_name),
		));
		$template->set_switch('style.selected', intval($group_info['group_style']) == $style_id);
	}
//-- fin mod : categories hierarchy --------------------------------------------

	$s_hidden_fields = '<input type="hidden" name="mode" value="' . $mode . '" /><input type="hidden" name="' . POST_GROUPS_URL . '" value="' . $group_id . '" />';

	$template->assign_vars(array(
		'GROUP_NAME' => $group_info['group_name'],
		'GROUP_DESCRIPTION' => $group_info['group_description'], 
		'GROUP_MODERATOR' => $group_moderator, 

		'L_GROUP_TITLE' => $lang['Group_administration'],
		'L_GROUP_EDIT_DELETE' => ( isset($HTTP_POST_VARS['new']) ) ? $lang['New_group'] : $lang['Edit_group'], 
		'L_GROUP_NAME' => $lang['group_name'],
		'L_GROUP_DESCRIPTION' => $lang['group_description'],
		'L_GROUP_MODERATOR' => $lang['group_moderator'], 
		'L_FIND_USERNAME' => $lang['Find_username'], 
		'L_GROUP_STATUS' => $lang['group_status'],
		'L_GROUP_OPEN' => $lang['group_open'],
		'L_GROUP_CLOSED' => $lang['group_closed'],
		'L_GROUP_HIDDEN' => $lang['group_hidden'],
		'L_GROUP_DELETE' => $lang['group_delete'],
		'L_GROUP_DELETE_CHECK' => $lang['group_delete_check'],
		'L_SUBMIT' => $lang['Submit'],
		'L_RESET' => $lang['Reset'],
		'L_DELETE_MODERATOR' => $lang['delete_group_moderator'],
		'L_DELETE_MODERATOR_EXPLAIN' => $lang['delete_moderator_explain'],
		'L_YES' => $lang['Yes'],

		'U_SEARCH_USER' => append_sid("../search.$phpEx?mode=searchuser"), 

		'S_GROUP_OPEN_TYPE' => GROUP_OPEN,
		'S_GROUP_CLOSED_TYPE' => GROUP_CLOSED,
		'S_GROUP_HIDDEN_TYPE' => GROUP_HIDDEN,
		'S_GROUP_OPEN_CHECKED' => $group_open,
		'S_GROUP_CLOSED_CHECKED' => $group_closed,
		'S_GROUP_HIDDEN_CHECKED' => $group_hidden,
		'S_GROUP_ACTION' => append_sid("admin_groups.$phpEx"),
		'S_HIDDEN_FIELDS' => $s_hidden_fields)
	);

	$template->pparse('body');

}
else if ( isset($HTTP_POST_VARS['group_update']) )
{
//-- mod : aucp colors ---------------------------------------------------------
//-- add
	// Read the users in the specified group
	$colors->read_group_users($group_id, ($mode == 'newgroup'));
//-- fin mod : aucp colors -----------------------------------------------------
	//
	// Ok, they are submitting a group, let's save the data based on if it's new or editing
	//
	if ( isset($HTTP_POST_VARS['group_delete']) )
	{
//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
		//
		// Reset User Moderator Level
		//

		// Is Group moderating a forum ?
		$sql = "SELECT auth_mod FROM " . AUTH_ACCESS_TABLE . " 
			WHERE group_id = " . $group_id;
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not select auth_access', '', __LINE__, __FILE__, $sql);
		}

		$row = $db->sql_fetchrow($result);
		if (intval($row['auth_mod']) == 1)
		{
			// Yes, get the assigned users and update their Permission if they are no longer moderator of one of the forums
			$sql = "SELECT user_id FROM " . USER_GROUP_TABLE . "
				WHERE group_id = " . $group_id;
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Could not select user_group', '', __LINE__, __FILE__, $sql);
			}

			$rows = $db->sql_fetchrowset($result);
			for ($i = 0; $i < count($rows); $i++)
			{
				$sql = "SELECT g.group_id FROM " . AUTH_ACCESS_TABLE . " a, " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug
				WHERE (a.auth_mod = 1) AND (g.group_id = a.group_id) AND (a.group_id = ug.group_id) AND (g.group_id = ug.group_id) 
					AND (ug.user_id = " . intval($rows[$i]['user_id']) . ") AND (ug.group_id <> " . $group_id . ")";
				if ( !($result = $db->sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, 'Could not obtain moderator permissions', '', __LINE__, __FILE__, $sql);
				}

				if ($db->sql_numrows($result) == 0)
				{
					$sql = "UPDATE " . USERS_TABLE . " SET user_level = " . USER . " 
					WHERE user_level = " . MOD . " AND user_id = " . intval($rows[$i]['user_id']);
					
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, 'Could not update moderator permissions', '', __LINE__, __FILE__, $sql);
					}
				}
			}
		}
*/
//-- add
		// check if it is a system group
		if ( $group_row['group_status'] == GROUP_SYSTEM )
		{
			$l_link = 'Click_return_groupsadmin';
			$u_link = $config->url('admin/admin_groups', array(POST_GROUPS_URL => intval($group_id)), true);
			message_return('Delete_sysgroup_denied', $l_link, $u_link);
		}

		// users to update
		$user_ids = array();

		// get all members of this group
		$sql = 'SELECT user_id
					FROM ' . USER_GROUP_TABLE . '
					WHERE group_id = ' . intval($group_id) . '
						AND user_pending <> 1';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			if ( !empty($row['user_id']) )
			{
				$user_ids[ intval($row['user_id']) ] = true;
			}
		}
		$db->sql_freeresult($result);

		// get users having auths specific to this group
		$sql = 'SELECT DISTINCT ug.user_id
					FROM ' . AUTHS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug
					WHERE ug.group_id = a.group_id
						AND ug.user_pending <> 1
						AND a.obj_type = \'' . POST_GROUPS_URL . '\'
						AND a.obj_id = ' . intval($group_id) . '
					GROUP BY ug.user_id';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$user_ids = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			if ( !empty($row['user_id']) )
			{
				$user_ids[ intval($row['user_id']) ] = true;
			}
		}
		$db->sql_freeresult($result);

		// delete auths relative to this group
		$sql = 'DELETE FROM ' . AUTHS_TABLE . '
					WHERE group_id = ' . intval($group_id) . '
						OR (obj_type = \'' . POST_GROUPS_URL . '\' AND obj_id = ' . intval($group_id) . ')';
		$db->sql_query($sql, false, __LINE__, __FILE__);

		// delete user caches
		if ( !empty($user_ids) )
		{
			$sql = 'DELETE FROM ' . USERS_CACHE_TABLE . '
						WHERE user_id IN(' . implode(', ', array_keys($user_ids)) . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
		unset($user_ids);

		// remove group style
		if ( intval($group_row['group_style']) )
		{
			$sql = 'UPDATE ' . GROUPS_TABLE . '
						SET group_style = 0
						WHERE group_user_id IN(' . $db->sql_subquery('user_id', '
								SELECT DISTINCT user_id
									FROM ' . USER_GROUP_TABLE . '
									WHERE group_id = ' . intval($group_id) . '
										AND user_pending <> 1
								', __LINE__, __FILE__) . ')
							AND group_style = ' . intval($group_row['group_style']);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		// reset the users mod status
		$moderators = new moderators();
		$moderators->set_users_status();
		unset($moderators);
//-- fin mod : categories hierarchy --------------------------------------------

		//
		// Delete Group
		//
		$sql = "DELETE FROM " . GROUPS_TABLE . "
			WHERE group_id = " . $group_id;
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not update group', '', __LINE__, __FILE__, $sql);
		}

		$sql = "DELETE FROM " . USER_GROUP_TABLE . "
			WHERE group_id = " . $group_id;
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not update user_group', '', __LINE__, __FILE__, $sql);
		}

//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
		$sql = "DELETE FROM " . AUTH_ACCESS_TABLE . "
			WHERE group_id = " . $group_id;
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not update auth_access', '', __LINE__, __FILE__, $sql);
		}
*/
//-- fin mod : categories hierarchy --------------------------------------------

//-- mod : aucp colors ---------------------------------------------------------
//-- add
		// reset the users in this group
		$colors->set_group_users($group_id, true);

		// Remove the column and do various tasks when removeing a group
		$colors->remove_group($group_id);
//-- fin mod : aucp colors -----------------------------------------------------

		$message = $lang['Deleted_group'] . '<br /><br />' . sprintf($lang['Click_return_groupsadmin'], '<a href="' . append_sid("admin_groups.$phpEx") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');

		message_die(GENERAL_MESSAGE, $message);
	}
	else
	{
		$group_type = isset($HTTP_POST_VARS['group_type']) ? intval($HTTP_POST_VARS['group_type']) : GROUP_OPEN;
		$group_name = isset($HTTP_POST_VARS['group_name']) ? htmlspecialchars(trim($HTTP_POST_VARS['group_name'])) : '';
		$group_description = isset($HTTP_POST_VARS['group_description']) ? trim($HTTP_POST_VARS['group_description']) : '';
		$group_moderator = isset($HTTP_POST_VARS['username']) ? $HTTP_POST_VARS['username'] : '';
		$delete_old_moderator = isset($HTTP_POST_VARS['delete_old_moderator']) ? true : false;

//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
		if ( $group_name == '' )
		{
			message_die(GENERAL_MESSAGE, $lang['No_group_name']);
		}
		else if ( $group_moderator == '' )
		{
			message_die(GENERAL_MESSAGE, $lang['No_group_moderator']);
		}
		
		$this_userdata = get_userdata($group_moderator, true);
		$group_moderator = $this_userdata['user_id'];

		if ( !$group_moderator )
		{
			message_die(GENERAL_MESSAGE, $lang['No_group_moderator']);
		}
*/
//-- add
		$group_style = _read('group_style', TYPE_INT, 0, $styles_available);
		$group_status = _read('group_status', TYPE_INT);
		$group_status_available = array_flip(array(GROUP_STANDARD, GROUP_SYSTEM));
		$sys_groups = array_flip(array(GROUP_FOUNDER, GROUP_ADMIN, GROUP_REGISTERED, GROUP_ANONYMOUS));

		// check the group system status value
		if ( !isset($group_status_available[$group_status]) )
		{
			_error('Unknown_group_sysstatus');
		}

		// check if we can remove this system status
		if ( isset($sys_groups[ intval($group_id) ]) && ($group_status != GROUP_SYSTEM) )
		{
			_error('Change_sysgroup_denied');
		}

		// check group type towards group status
		if ( (intval($group_status) >= GROUP_SYSTEM) && ($group_type < GROUP_CLOSED) )
		{
			_error('Change_sysgroup_type_denied');
		}

		// send error
		if ( $error )
		{
			$l_link = 'Click_return_groupsadmin';
			$u_link = $config->url('admin/admin_groups', array(POST_GROUPS_URL => intval($group_id)), true);
			message_return($error_msg, $l_link, $u_link);
		}

		if ( $group_name == '' )
		{
			message_die(GENERAL_MESSAGE, 'No_group_name');
		}
		else if ( empty($group_moderator) && ($mode == 'editgroup') && ($group_id == GROUP_FOUNDER) )
		{
			message_die(GENERAL_MESSAGE, 'No_group_moderator');
		}

		if ( !empty($group_moderator) )
		{
			$this_userdata = get_userdata($group_moderator, true);
			if ( !$group_moderator = $this_userdata['user_id'] )
			{
				message_die(GENERAL_MESSAGE, 'No_group_moderator');
			}
		}
		else
		{
			$group_moderator = 0;
		}
//-- fin mod : categories hierarchy --------------------------------------------
				
		if( $mode == "editgroup" )
		{
			$sql = "SELECT *
				FROM " . GROUPS_TABLE . "
				WHERE group_single_user <> " . TRUE . "
				AND group_id = " . $group_id;
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Error getting group information', '', __LINE__, __FILE__, $sql);
			}

			if( !($group_info = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_MESSAGE, $lang['Group_not_exist']);
			}
		
//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
			if ( $group_info['group_moderator'] != $group_moderator )
			{
				if ( $delete_old_moderator )
				{
					$sql = "DELETE FROM " . USER_GROUP_TABLE . "
						WHERE user_id = " . $group_info['group_moderator'] . " 
							AND group_id = " . $group_id;
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, 'Could not update group moderator', '', __LINE__, __FILE__, $sql);
					}
				}

				$sql = "SELECT user_id 
					FROM " . USER_GROUP_TABLE . " 
					WHERE user_id = $group_moderator 
						AND group_id = $group_id";
				if ( !($result = $db->sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, 'Failed to obtain current group moderator info', '', __LINE__, __FILE__, $sql);
				}

				if ( !($row = $db->sql_fetchrow($result)) )
				{
					$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending)
						VALUES (" . $group_id . ", " . $group_moderator . ", 0)";
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, 'Could not update group moderator', '', __LINE__, __FILE__, $sql);
					}
				}
			}

			$sql = "UPDATE " . GROUPS_TABLE . "
				SET group_type = $group_type, group_name = '" . str_replace("\'", "''", $group_name) . "', group_description = '" . str_replace("\'", "''", $group_description) . "', group_moderator = $group_moderator 
				WHERE group_id = $group_id";
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not update group', '', __LINE__, __FILE__, $sql);
			}
*/
//-- add
			if ( $group_info['group_moderator'] != $group_moderator )
			{
				if ( $delete_old_moderator )
				{
					if ( intval($group_info['group_moderator']) )
					{
						// recache old moderator
						$sql = 'DELETE FROM ' . USERS_CACHE_TABLE . '
									WHERE user_id = ' . intval($group_info['group_moderator']);
						$db->sql_query($sql, false, __LINE__, __FILE__);

						// remove style if any
						if ( intval($group_row['group_style']) )
						{
							$sql = 'UPDATE ' . GROUPS_TABLE . '
										SET group_style = 0
										WHERE group_user_id = ' . intval($group_info['group_moderator']) . '
											AND group_style = ' . intval($group_row['group_style']);
							$db->sql_query($sql, false, __LINE__, __FILE__);
						}

						// remove the link to the group
						$sql = 'DELETE FROM ' . USER_GROUP_TABLE . '
									WHERE user_id = ' . intval($group_info['group_moderator']) . '
										AND group_id = ' . intval($group_id);
						$db->sql_query($sql, false, __LINE__, __FILE__);
					}
				}

				// add the moderator to the group
				if ( intval($group_moderator) )
				{
					$sql = 'SELECT user_id
								FROM ' . USER_GROUP_TABLE . '
								WHERE user_id = ' . intval($group_moderator) . '
									AND group_id = ' . intval($group_id);
					$result = $db->sql_query($sql, false, __LINE__, __FILE__);
					$found = ($row = $db->sql_fetchrow($result));
					$db->sql_freeresult($result);
					if ( !$found )
					{
						$fields = array(
							'user_id' => intval($group_moderator),
							'group_id' => intval($group_id),
							'user_pending' => 0,
						);
						$db->sql_statement($fields);
						$sql = 'INSERT INTO ' . USER_GROUP_TABLE . '
									(' . $db->sql_fields . ') VALUES(' . $db->sql_values . ')';
						$db->sql_query($sql, false, __LINE__, __FILE__);
					}
				}
			}

			// update group
			$fields = array(
				'group_type' => intval($group_type),
				'group_status' => intval($group_status),
				'group_style' => intval($group_style),
				'group_name' => stripslashes($group_name),
				'group_description' => stripslashes($group_description),
				'group_moderator' => intval($group_moderator),
			);
			$db->sql_statement($fields);
			$sql = 'UPDATE ' . GROUPS_TABLE . '
						SET ' . $db->sql_update . '
						WHERE group_id = ' . intval($group_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);

			// recache group moderator
			if ( intval($group_moderator) )
			{
				$sql = 'DELETE FROM ' . USERS_CACHE_TABLE . '
							WHERE user_id = ' . intval($group_moderator);
				$db->sql_query($sql, false, __LINE__, __FILE__);
			}

			// set group style
			$old_style = intval($group_row['group_style']);
			$new_style = intval($group_style);
			if ( $old_style || $new_style )
			{
				$sql = 'UPDATE ' . GROUPS_TABLE . '
							SET group_style = ' . $new_style . '
							WHERE group_style = ' . $old_style . '
								AND group_user_id IN(' . $db->sql_subquery('user_id', '
									SELECT DISTINCT user_id
										FROM ' . USER_GROUP_TABLE . '
										WHERE user_pending = 0
											AND group_id = ' . intval($group_id) . '
								', __LINE__, __FILE__) . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);
			}

			// reset the users mod status
			$moderators = new moderators();
			$moderators->set_users_status();
			unset($moderators);

			// reset bots
			$bots = new bots();
			$bots->read(true);
			unset($bots);

//-- mod : aucp colors ---------------------------------------------------------
//-- add
			// reset the users in this group
			$colors->set_group_users($group_id);
//-- fin mod : aucp colors -----------------------------------------------------

//-- fin mod : categories hierarchy --------------------------------------------
	
			$message = $lang['Updated_group'] . '<br /><br />' . sprintf($lang['Click_return_groupsadmin'], '<a href="' . append_sid("admin_groups.$phpEx") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');;

			message_die(GENERAL_MESSAGE, $message);
		}
		else if( $mode == 'newgroup' )
		{
//-- mod : categories hierarchy ------------------------------------------------
//-- delete
/*
			$sql = "INSERT INTO " . GROUPS_TABLE . " (group_type, group_name, group_description, group_moderator, group_single_user) 
				VALUES ($group_type, '" . str_replace("\'", "''", $group_name) . "', '" . str_replace("\'", "''", $group_description) . "', $group_moderator,	'0')";
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not insert new group', '', __LINE__, __FILE__, $sql);
			}
			$new_group_id = $db->sql_nextid();

			$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending)
				VALUES ($new_group_id, $group_moderator, 0)";
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not insert new user-group info', '', __LINE__, __FILE__, $sql);
			}
*/
//-- add
			$fields = array(
				'group_type' => intval($group_type),
				'group_status' => intval($group_status),
				'group_style' => intval($group_style),
				'group_name' => stripslashes($group_name),
				'group_description' => stripslashes($group_description),
				'group_moderator' => intval($group_moderator),
				'group_single_user' => 0,
			);
			$db->sql_statement($fields);
			$sql = 'INSERT INTO ' . GROUPS_TABLE . '
						(' . $db->sql_fields . ') VALUES(' . $db->sql_values . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
			$new_group_id = $db->sql_nextid();

			if ( intval($group_moderator) )
			{
				$fields = array(
					'group_id' => intval($new_group_id),
					'user_id' => intval($group_moderator),
					'user_pending' => 0,
				);
				$db->sql_statement($fields);
				$sql = 'INSERT INTO ' . USER_GROUP_TABLE . '
							(' . $db->sql_fields . ') VALUES(' . $db->sql_values . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);

				// recache group moderator
				$sql = 'DELETE FROM ' . USERS_CACHE_TABLE . '
							WHERE user_id = ' . intval($group_moderator);
				$db->sql_query($sql, false, __LINE__, __FILE__);

				// set style
				if ( intval($group_style) )
				{
					$sql = 'UPDATE ' . GROUPS_TABLE . '
								SET group_style = ' . intval($group_style) . '
								WHERE group_style = 0
									AND group_user_id = ' . intval($group_moderator);
					$db->sql_query($sql, false, __LINE__, __FILE__);
				}

				// reset the users mod status
				$moderators = new moderators();
				$moderators->set_users_status();
				unset($moderators);

				// reset bots
				$bots = new bots();
				$bots->read(true);
				unset($bots);
			}
//-- fin mod : categories hierarchy --------------------------------------------

//-- mod : aucp colors ---------------------------------------------------------
//-- add
			// Add the column and do various tasks when adding a new group
			$colors->add_group($new_group_id);

			// reset the users in this group
			$colors->set_group_users($new_group_id);
//-- fin mod : aucp colors -----------------------------------------------------
			
			$message = $lang['Added_new_group'] . '<br /><br />' . sprintf($lang['Click_return_groupsadmin'], '<a href="' . append_sid("admin_groups.$phpEx") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');;

			message_die(GENERAL_MESSAGE, $message);

		}
		else
		{
			message_die(GENERAL_MESSAGE, $lang['No_group_action']);
		}
	}
}
else
{
	$sql = "SELECT group_id, group_name
		FROM " . GROUPS_TABLE . "
		WHERE group_single_user <> " . TRUE . "
		ORDER BY group_name";
//-- mod : categories hierarchy ------------------------------------------------
//-- add
	$sql = str_replace('SELECT ', 'SELECT group_status, ', $sql);
	$sql = str_replace('WHERE ', 'WHERE group_status < ' . GROUP_SPECIAL . ' AND ', $sql);
//-- fin mod : categories hierarchy --------------------------------------------
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not obtain group list', '', __LINE__, __FILE__, $sql);
	}

	$select_list = '';
	if ( $row = $db->sql_fetchrow($result) )
	{
		$select_list .= '<select name="' . POST_GROUPS_URL . '">';
		do
		{
//-- mod : categories hierarchy ------------------------------------------------
//-- add
			if ( $row['group_status'] == GROUP_SYSTEM )
			{
				$row['group_name'] = $user->lang($row['group_name']);
			}
//-- fin mod : categories hierarchy --------------------------------------------
			$select_list .= '<option value="' . $row['group_id'] . '">' . $row['group_name'] . '</option>';
		}
		while ( $row = $db->sql_fetchrow($result) );
		$select_list .= '</select>';
	}

	$template->set_filenames(array(
		'body' => 'admin/group_select_body.tpl')
	);

	$template->assign_vars(array(
		'L_GROUP_TITLE' => $lang['Group_administration'],
		'L_GROUP_EXPLAIN' => $lang['Group_admin_explain'],
		'L_GROUP_SELECT' => $lang['Select_group'],
		'L_LOOK_UP' => $lang['Look_up_group'],
		'L_CREATE_NEW_GROUP' => $lang['New_group'],

		'S_GROUP_ACTION' => append_sid("admin_groups.$phpEx"),
		'S_GROUP_SELECT' => $select_list)
	);

	if ( $select_list != '' )
	{
		$template->assign_block_vars('select_box', array());
	}

	$template->pparse('body');
}

include('./page_footer_admin.'.$phpEx);

?>